﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using QuantumServices.MailService;
using QuantumWorkspaces.DomainEntities;
using Raven.Client;
using Raven.Client.Document;

namespace QuantumServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MailServices" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select MailServices.svc or MailServices.svc.cs at the Solution Explorer and start debugging.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
                 ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class MailServices : IMailService
    {

        private static IDocumentStore dbStore;

        private static IDocumentStore DbStore
        {
            get
            {
                if (dbStore == null)
                {
                    dbStore = new DocumentStore { Url = "http://data.klowdi.com/", DefaultDatabase = "QuantumUsers" };
                    dbStore.Initialize();
                }
                return dbStore;
            }
        }


        public bool SendUserPasswordResendMail(string strUserName)
        {
            using (var session = DbStore.OpenSession())
            {
                QuantumUser foundUser = session.Query<QuantumUser>()
                                               .Where(x => x.User == strUserName)
                                               .FirstOrDefault();

                if (foundUser == null)
                    return false;
                else
                {
                    string strPassword = foundUser.Password;
                    string strUserMail = foundUser.Email;
                    ResendMailMessage aResendPassword = new ResendMailMessage();
                    aResendPassword.Password = strPassword;
                    aResendPassword.UserName = strUserName;
                    MailUtils.SendMail(aResendPassword, strUserMail);
                }
            }
            return true;
        }

        public bool SendUserPasswordConfirmationMail(string strUserName)
        {
            using (var session = DbStore.OpenSession())
            {
                QuantumUser foundUser = session.Query<QuantumUser>()
                                               .Where(x => x.User == strUserName)
                                               .FirstOrDefault();

                if (foundUser == null)
                    return false;
                else
                {
                    string strPassword = foundUser.Password;
                    string strUserMail = foundUser.Email;
                    PasswordConfirmationMessage aPasswordConfirmationMessage = new PasswordConfirmationMessage();
                    aPasswordConfirmationMessage.Password = strPassword;
                    // aPasswordConfirmationMessage.UserName = strUserName;
                    MailUtils.SendMail(aPasswordConfirmationMessage, strUserMail);
                }
            }
            return true;
        }
    }
}
